const express = require('express');
const router = express.Router();
// 引入数据库
const db = require('../config/database');
// 引入jwt工具包
const {createToken} = require("../config/jwtUtil");

router.post('/login', (req, res) => {
    // 获取请求体数据
    const {username,password} = req.body;
    // 根据用户名和密码查询数据库
    db.query(`SELECT * FROM tb_user WHERE username = ? AND password = ?`, [username, password], (err, result) => {
        if (err) throw err;
        if (result.length > 0) {
            // 生成 JWT 字符串
            const token = createToken({id:result[0].id});
            res.json({token});
        } else {
            setTimeout(() => {
                res.json(new Error("账号或密码错误"))
            },2000)
        }
    })
});

router.get('/list', (req, res) => {
    res.json([{token:1},{token:2}])
})


module.exports = router